<?php
namespace Admin\Controller;

class AppController extends \Admin\Controller\AdminController
{
	/**
	 * 查询APP列表
	 * @author 刘新
	 */
	public function appList()
	{
		$con = I('condition');
		$where['status'] = array('egt', 0);
		
		if($con) {
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			}
		}
		
		$list = $this->lists('App', $where, 'platform,app_version desc');
		if($list) {
			int_to_string($list);
			foreach($list as $key => &$val) {
				$val['update_type_text'] = C('APP_UPDATE_TYPE')[$val['update_type']];
				$val['platform_text'] = C('APP_PLATFORM')[$val['platform']];
				$val['update_status_text'] = C('APP_UPDATE_STATUS')[$val['update_status']];
			}
		}
		$this->assign('_list', $list);
		$this->display('list');
	}
	
	/**
	 * 添加APP
	 * @author 刘新
	 */
	public function addApp()
	{
		if(IS_GET) {
			//查询APP
			$this->assign('_title', '新增APP');
			$this->display('add_edit');
		}

		$res = D('App')->addApp(I('param.'));
		if($res['error'] === 0) $this->success($res['info'], U('App/appList'));
		$this->error($res['info']);
	}
	
	/**
	 * 编辑APP
	 * @author 刘新
	 */
	public function editApp()
	{
		$id = I('id', '', '/^\d+$/');
		if(!$id) $this->error('参数错误');
		
		if(IS_GET) {
			//查询APP
			$app = $this->returnOne('App', array('id' => $id, 'status' => array('gt', -1)));
			if(!$app) $this->error('APP查询失败');
			$this->assign('_title', '编辑APP');
			$this->assign('app', $app);
			$this->display('add_edit');
		}
	
		$res = D('App')->editApp(I('param.'));
		if($res['error'] === 0) $this->success($res['info'], U('App/appList'));
		$this->error($res['info']);
	}
	
	/**
	 * 查询APP文件信息
	 * @author 刘新
	 */
	public function fileInfo()
	{
		$platform = I('platform', '', '/^1\d{2}$/');
		if(!$platform) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		$list =  $this->returnList('File', array('file_type' => $platform), '', 'id,name,url,ext');
		if(!$list) $this->ajaxReturn(array('error' => 1, 'info' => '查询失败'));
		$this->ajaxReturn(array('error' => 0, 'info' => $list));
	}
	
	/**
	 * APP详情
	 * @author 刘新
	 */
	public function appDetail()
	{
		$id = I('id', '', '/^\d+$/');
		if(!$id) $this->error('参数错误');
		
		$app = $this->returnOne('App', array('id' => $id, 'status' => array('egt', -1)));
		if(!$app) $this->error('查询失败');
		$app['update_type_text'] = C('APP_UPDATE_TYPE')[$app['update_type']];
		$app['platform_text'] = C('APP_PLATFORM')[$app['platform']];
		$app['update_status_text'] = C('APP_UPDATE_STATUS')[$app['update_status']];
		
		//查询文件信息
		if($app['file_id']) {
			$file = $this->returnOne('File', array('id' => $app['file_id']), '', 'id,name,url,realpath');
			if(!$file) $this->error('APP文件信息查询失败');
			$app['file'] = $file;
		}
		
		$app = array($app);
		int_to_string($app);

		$this->assign('_title', 'APP详情');
		$this->assign('app', $app[0]);
		$this->display('detail');
	}
	
	/**
	 * API状态修改
	 * @author 刘新
	 */
	public function changeStatus($method=null)
	{
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) $this->error('请选择要操作的数据!');
		$map['uid'] =   array('in',$id);
	
		switch ( strtolower($method) )
		{
			case 'forbid':
				$this->forbid('App', $map );
				break;
			case 'resume':
				$this->resume('App', $map );
				break;
			case 'delete':
				$this->delete('App', $map );
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	/**
	 * 启用APP，同时禁用同平台的其它APP
	 * @author 刘新
	 */
	public function enableApp()
	{
		$id = I('id', '', '/^\d+$/');
		$platform = I('platform', '', '/^\d+$/');
		if(!$id || !$platform) $this->error('参数错误');
		
		//禁用同平台的APP
		$where['platform'] = $platform;
		$where['status'] = 1;
		$app = M('App');
		$app->where($where)->save(array('status' => 0));
		
		//启用指定APP
		$res = $app->where(array('id' => $id))->save(array('status' => 1));
		if(!$res) $this->error('启用失败');
		$this->success('启用成功', U('App/appList'));
	}
}